package com.xzy.web.controller;

import com.xzy.custom.LogUtils;
import com.xzy.entities.BorrowRecord;
import com.xzy.entities.SysUser;
import com.xzy.security.UserUtils;
import com.xzy.service.BorrowRecordService;
import com.xzy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO70
 *
 * @author XiangZhongyi
 * ClassName: 用户登录
 * date: 2021/9/21 17:02
 */
@Controller
public class LoginController {

    @Autowired
    private LogUtils logUtils;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private UserUtils userUtils;
    @Autowired
    private BorrowRecordService borrowService;

    @RequestMapping(value = {"/","/index"})
    public String index(){
        return "index";
    }

    @RequestMapping(value = "/admin")
    public String login(ModelMap map){
        SysUser user = userUtils.getUser();
        if (user != null) {
            logUtils.log(user.getUsername(), "登陆");
            return "admin/main";
        }
        return "redirect:login";
    }

    @RequestMapping("/home/manage")
    public String home(ModelMap map){
        List<BorrowRecord> list = borrowService.findAll();
        List<BorrowRecord> borrow = borrowService.findAllDeWeight();
        List<BorrowRecord> borrow1 = new ArrayList<>();
        for (BorrowRecord borrowRecord : list) {
            if (!borrowRecord.getStatus().equals("已归还")) {
                borrow1.add(borrowRecord);
            }
        }
        map.put("borrow", borrow.size());
        map.put("borrow1", borrow1.size());
        return "admin/home";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:logout";
    }
}
